home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 21 Ambient Occlusion / Ssao / Shaders / Common.hlsl next >
Encoding:
Text File  |  2016-03-02  |  4.5 KB  |  151 lines

  1. //***************************************************************************************
  2. // Common.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. // Defaults for number of lights.
  6. #ifndef NUM_DIR_LIGHTS
  7.     #define NUM_DIR_LIGHTS 3
  8. #endif
  9.  
  10. #ifndef NUM_POINT_LIGHTS
  11.     #define NUM_POINT_LIGHTS 0
  12. #endif
  13.  
  14. #ifndef NUM_SPOT_LIGHTS
  15.     #define NUM_SPOT_LIGHTS 0
  16. #endif
  17.  
  18. // Include structures and functions for lighting.
  19. #include "LightingUtil.hlsl"
  20.  
  21. struct MaterialData
  22. {
  23.     float4   DiffuseAlbedo;
  24.     float3   FresnelR0;
  25.     float    Roughness;
  26.     float4x4 MatTransform;
  27.     uint     DiffuseMapIndex;
  28.     uint     NormalMapIndex;
  29.     uint     MatPad1;
  30.     uint     MatPad2;
  31. };
  32.  
  33. TextureCube gCubeMap : register(t0);
  34. Texture2D gShadowMap : register(t1);
  35. Texture2D gSsaoMap   : register(t2);
  36.  
  37. // An array of textures, which is only supported in shader model 5.1+.  Unlike Texture2DArray, the textures
  38. // in this array can be different sizes and formats, making it more flexible than texture arrays.
  39. Texture2D gTextureMaps[10] : register(t3);
  40.  
  41. // Put in space1, so the texture array does not overlap with these resources.  
  42. // The texture array will occupy registers t0, t1, ..., t3 in space0. 
  43. StructuredBuffer<MaterialData> gMaterialData : register(t0, space1);
  44.  
  45.  
  46. SamplerState gsamPointWrap        : register(s0);
  47. SamplerState gsamPointClamp       : register(s1);
  48. SamplerState gsamLinearWrap       : register(s2);
  49. SamplerState gsamLinearClamp      : register(s3);
  50. SamplerState gsamAnisotropicWrap  : register(s4);
  51. SamplerState gsamAnisotropicClamp : register(s5);
  52. SamplerComparisonState gsamShadow : register(s6);
  53.  
  54. // Constant data that varies per frame.
  55. cbuffer cbPerObject : register(b0)
  56. {
  57.     float4x4 gWorld;
  58.     float4x4 gTexTransform;
  59.     uint gMaterialIndex;
  60.     uint gObjPad0;
  61.     uint gObjPad1;
  62.     uint gObjPad2;
  63. };
  64.  
  65. // Constant data that varies per material.
  66. cbuffer cbPass : register(b1)
  67. {
  68.     float4x4 gView;
  69.     float4x4 gInvView;
  70.     float4x4 gProj;
  71.     float4x4 gInvProj;
  72.     float4x4 gViewProj;
  73.     float4x4 gInvViewProj;
  74.     float4x4 gViewProjTex;
  75.     float4x4 gShadowTransform;
  76.     float3 gEyePosW;
  77.     float cbPerObjectPad1;
  78.     float2 gRenderTargetSize;
  79.     float2 gInvRenderTargetSize;
  80.     float gNearZ;
  81.     float gFarZ;
  82.     float gTotalTime;
  83.     float gDeltaTime;
  84.     float4 gAmbientLight;
  85.  
  86.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  87.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  88.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  89.     // are spot lights for a maximum of MaxLights per object.
  90.     Light gLights[MaxLights];
  91. };
  92.  
  93. //---------------------------------------------------------------------------------------
  94. // Transforms a normal map sample to world space.
  95. //---------------------------------------------------------------------------------------
  96. float3 NormalSampleToWorldSpace(float3 normalMapSample, float3 unitNormalW, float3 tangentW)
  97. {
  98.     // Uncompress each component from [0,1] to [-1,1].
  99.     float3 normalT = 2.0f*normalMapSample - 1.0f;
  100.  
  101.     // Build orthonormal basis.
  102.     float3 N = unitNormalW;
  103.     float3 T = normalize(tangentW - dot(tangentW, N)*N);
  104.     float3 B = cross(N, T);
  105.  
  106.     float3x3 TBN = float3x3(T, B, N);
  107.  
  108.     // Transform from tangent space to world space.
  109.     float3 bumpedNormalW = mul(normalT, TBN);
  110.  
  111.     return bumpedNormalW;
  112. }
  113.  
  114. //---------------------------------------------------------------------------------------
  115. // PCF for shadow mapping.
  116. //---------------------------------------------------------------------------------------
  117. //#define SMAP_SIZE = (2048.0f)
  118. //#define SMAP_DX = (1.0f / SMAP_SIZE)
  119. float CalcShadowFactor(float4 shadowPosH)
  120. {
  121.     // Complete projection by doing division by w.
  122.     shadowPosH.xyz /= shadowPosH.w;
  123.  
  124.     // Depth in NDC space.
  125.     float depth = shadowPosH.z;
  126.  
  127.     uint width, height, numMips;
  128.     gShadowMap.GetDimensions(0, width, height, numMips);
  129.  
  130.     // Texel size.
  131.     float dx = 1.0f / (float)width;
  132.  
  133.     float percentLit = 0.0f;
  134.     const float2 offsets[9] =
  135.     {
  136.         float2(-dx,  -dx), float2(0.0f,  -dx), float2(dx,  -dx),
  137.         float2(-dx, 0.0f), float2(0.0f, 0.0f), float2(dx, 0.0f),
  138.         float2(-dx,  +dx), float2(0.0f,  +dx), float2(dx,  +dx)
  139.     };
  140.  
  141.     [unroll]
  142.     for(int i = 0; i < 9; ++i)
  143.     {
  144.         percentLit += gShadowMap.SampleCmpLevelZero(gsamShadow,
  145.             shadowPosH.xy + offsets[i], depth).r;
  146.     }
  147.     
  148.     return percentLit / 9.0f;
  149. }
  150.  
  151.